﻿using System;
using ZFine.Application.Gas;
using ZFine.Application.SystemManage;
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using ZFine.Domain.Entity.SystemManage;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;

namespace ZFine.Web.Areas.Gas.Controllers
{
    public class PwdKeyController : ControllerBase
    {
        private PwdKeyApp PwdKeyApp = new PwdKeyApp();

        [HttpGet]
        [HandlerAuthorize(ignore: false)]
        public override ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(ignore: false)]
        public override ActionResult Form()
        {
            return View();
        }
        [HandlerAuthorize(ignore: false)]
        public override ActionResult Details()
        {
            return View();
        }
        [HandlerAuthorize(ignore: false)]
        public ActionResult Update()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination)
        {
            List<PwdKeyEntity> l_class = PwdKeyApp.GetIQueryable().ToList();
            var data = new
            {
                page = pagination.page,//当前页  1  2 3 之类
                records = pagination.records,//总条数  这里应该是18
                rows = l_class,//分页的数据
                total = pagination.total,//页数
            };
            return Content(data.ToJson());
        }

        //修改页数据
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJsonForUpdate(string keyValue)
        {
            PwdKeyEntity data = PwdKeyApp.GetForm(keyValue);
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(PwdKeyEntity pwdKeyEntity, string keyValue)
        {
            getPwdKeyByIDNumber(pwdKeyEntity);
            PwdKeyApp.SubmitForm(pwdKeyEntity, keyValue);
            return Success("操作成功。");
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize(ignore: false)]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            PwdKeyApp.Del(keyValue);
            return Success("删除成功。");
        }

        // 申请密钥生成（特殊申请的情况）
        private void getPwdKeyByIDNumber(PwdKeyEntity pwdKeyEntity)
        {
            if (null != pwdKeyEntity)
            {
                //密钥生成
                string newPwdKey = Md5.md5(pwdKeyEntity.IDNumber, 16).ToLower();
                string sixPwdKey = newPwdKey.Substring(newPwdKey.Length - 6);
                pwdKeyEntity.PwdKey = sixPwdKey;
            }
        }
    }
}
